<?php
/**
 * 这个是客户购买的中文分词查询接口，
 * 购买链接 https://market.aliyun.com/products/57124001/cmapi018397.html#sku=yuncode1239700000
 * 服务商为：昆明秀派科技有限公司
 */
namespace com;

class SepWord {
    
    const HOST = "http://showapifc.market.alicloudapi.com";
   
    protected static $appcode = null;
    
    //设置APPcode
    public static function setAppcode($appcode){
        self::$appcode  = $appcode;
    }

    /**
     * 查询中文分词
     * @param string $text 文本
     */
    public static function query($text){
       
        $bodys = 'text=' . $text;
        return self::curl("/sepWord",$bodys);
    }
   
     //发送请求
    private static function curl($path,$bodys){    

        $url = self::HOST . $path;

        $headers = array();
        array_push($headers, "Authorization:APPCODE " . self::$appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");

        $method = "POST";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".self::HOST, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $result = (curl_exec($curl));

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
            // 通过两个回车换行来分割header和body
            list($header, $body) = explode("\r\n\r\n", $result, 2);
        }

        $body = json_decode($body, true);

        if ($body['showapi_res_code'] != 0) {
            \think\facade\Log::info('分词请求失败：'.$body['showapi_res_error']);
            throw new \Exception("请求失败");
        }
        if ($httpCode == 200) {
            return $body['showapi_res_body'];
        }
        if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
            throw new \Exception("参数错误");
        }
        if ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
             throw new \Exception("AppCode错误");
        }
        If ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
             throw new \Exception("请求的 Method、Path 或者环境错误");
        }
        if ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
             throw new \Exception("服务未被授权（或URL和Path不正确）");
        }
        if ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
             throw new \Exception("套餐包次数用完");
        }
        if ($httpCode == 500) {
             throw new \Exception("API网关错误");
        }
        if ($httpCode == 0) {
             throw new \Exception("URL错误");
        }

        throw new \Exception("参数名错误 或 其他错误");
    }
}